Loading, please wait...

A to Z Full Forms and Acronyms

Lets play Stone- Paper-Scissor  With JavaScript

The basic idea revolves under the functionality of different keywords and defined functions. The core to produce a game of Stone-Paper-Scissor with JavaScript depends on the calling of functions and getting random values.

The basic idea revolves around the functionality of different keywords and defined functions. The core to produce a game of Stone-Paper-Scissor with JavaScript depends on the calling of functions and getting random values.

Random Value Generation

It is important to define a function that will generate some values when called and this should not be some predefined values. That is why we need to generate some unpredictable and random values from our functions.

There is a  function Math.random(), which is used to return a floating-point pseudo-random number.

It ranges [0,1) , 0 (inclusive) and 1 (exclusive). According to the desired range, these random numbers can be scaled.

After declaring Math.random (), assign the values as Stone, Paper, and Scissor on the basis of the ranges.


For example:

If the value is less than 0.3 it will return Scissors if between 0.3 to 0.6 then returns Paper otherwise will return Stone

The function to generate random values and assigning them Stone, Paper, and Scissor on the basis of their range.

Comparing Given Input vs Returned Value

Define a function that will compare the returned value, which was generated due to Math.random ( ) and the values which will be provided by the user.

Conditional rendering is needed for this function.

The conditions will be: If the values are the same then it is a draw otherwise, provide necessary conditions to display whether it is a win or a loose.

For example :

If the user input value is Scissors and randomly generated value is not stone then the user wins or else it is loose.

The function to compare the values a deciding whether it is loose or win.

Calling the Function and Printing it

After defining all the functions, just simply call them and print them using console.log(function_name("Parameter")).

The parameter should contain Stone- Paper-Scissor  With JavaScript only.

NOTE: These are case sensitive and should be between inverted commas.

The part /*your words*/ should contain Stone paper or Scissor only.

For full code and assistance visit the repository.

A to Z Full Forms and Acronyms

Related Article